package com.dengke.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.dengke.hibernate.model.Address;
import com.dengke.hibernate.model.Student;
import com.dengke.hibernate.model.University;

public class Main {

	private static SessionFactory factory;

	public static void main(String[] args) {
		// try {
		// factory=new Configuration().configure().buildSessionFactory();
		// } catch (Throwable e) {
		// System.err.println("Failed to create sessionfactory object."+e);
		// throw new ExceptionInInitializerError(e);
		// }

		// try {
		// factory = new AnnotationConfiguration()
		// .configure()
		// .addPackage("com.dengke.hibernate")
		// .addAnnotatedClass(EmployeeNew.class)
		// .buildSessionFactory();
		// factory=new Configuration().configure().buildSessionFactory();
		// } catch (HibernateException e) {
		// System.err.println("Failed to create Sessionfactory object."+e);
		// throw new ExceptionInInitializerError();
		// }

		// Employee employee=new Employee("Zara", "Ali", 1000);
		// Employee employee2=new Employee("Daisy", "Das", 5000);
		// Employee employee3=new Employee("John", "Paul", 10000);

		// ManageEmployee manage = new ManageEmployee(factory);
		// manage.addEmployee(employee);
		// manage.addEmployee(employee2);
		// manage.addEmployee(employee3);
		// manage.listEmployee();
		// manage.updateEmployee(1, 2000);
		// manage.listEmployee();

		// factory=new
		// AnnotationConfiguration().configure().buildSessionFactory();
		factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();

		// Student student=new Student("Duke", "Disilva", "Maths");
		Address address = new Address("11 Silver street", "NYC", "USA");

		Student student = new Student("Sam", "Disilva", "Maths");
		Student student2 = new Student("Joshua", "Brill", "Science");
		Student student3 = new Student("Peter", "Pan", "Physics");

		University university = new University("Lit", "China");

		student.setUniversity(university);
		student2.setUniversity(university);
		student3.setUniversity(university);

		session.beginTransaction();
		// one-to-one student->address
		// session.persist(student);
		// address.setId(student.getId());
		// student.setAddress(address);
//		student.setAddress(address);
//		student2.setAddress(address);
//		student3.setAddress(address);
//		address.setStudent(student);

		session.persist(university);
		session.persist(student);
		session.persist(student2);
		session.persist(student3);
//		session.save(student);
		
		session.getTransaction().commit();
		List<Student> students = session.createQuery("from Student").list();
		for (Student stu : students) {
			System.out.println(stu.toString());
			System.out.println(stu.getUniversity().toString());
		}
	}

}
